<template>
  <div class="article-list-component" v-loading="loading">
    <transition-group name="post-list" tag="div" class="posts-list">
      <article v-for="(post, index) in articles" :key="post.id" class="post-item"
        :class="{ 'post-item-odd': index % 2 !== 0, 'post-item-even': index % 2 === 0 }">
        <div class="post-content">
          <div class="post-main">
            <div class="post-text-wrapper">
              <div class="post-text">
                <h3>
                  <span v-if="post.isStick" class="stick-tag">
                    <i class="fas fa-thumbtack"></i>
                    置顶
                  </span>
                  <span class="post-title underline" @click="$emit('article-click', post.id)">{{ post.title }}</span>
                </h3>
                <el-popover
                  placement="top"
                  width="400"
                  trigger="click"
                  :content="post.summary"
                  popper-class="summary-popover"
                >
                  <p slot="reference" class="post-excerpt">{{ post.summary }}</p>
                </el-popover>
                <div class="post-meta">
                  <span class="category-tag" @click.stop="handleCategoryClick(post.categoryName)">{{ post.categoryName
                  }}</span>
                  <span class="read-time">
                    <i class="far fa-clock"></i>
                    {{ Math.ceil((post.contentMd && post.contentMd.split) ? post.contentMd.split(" ").length / 300 : 1)
                    }}分钟阅读
                  </span>
                </div>
                <div class="read-more" @click="$emit('article-click', post.id)">
                  阅读全文
                  <i class="el-icon-arrow-right"></i>
                </div>
              </div>
            </div>
            <div class="post-image" @click="$emit('article-click', post.id)">
              <div class="image-container">
                <img v-lazy="post.cover" :key="post.cover" :alt="post.title">
                <div class="image-overlay"></div>
                <div class="read-more-on-image">
                  <i class="el-icon-view"></i>
                  <span>阅读全文</span>
                </div>
              </div>
            </div>
          </div>

          <div class="post-footer">
            <el-popover placement="top" trigger="click" :visible-arrow="false" popper-class="user-popover"
              v-model="popoverVisible[post.id]">
              <user-profile-card v-if="popoverVisible[post.id]" :user-id="post.userId"
                @close-popover="closePopover(post.id)" />
              <div class="author-info" slot="reference">
                <el-avatar :size="24" :src="post.avatar || ''"></el-avatar>
                <span class="author-name">{{ post.nickname }}</span>
              </div>
            </el-popover>
            <div class="post-info">
              <div class="post-date">
                <i class="far fa-calendar"></i>
                {{ formatTime(post.createTime) }}
              </div>
              <div class="post-view">
                <i class="far fa-eye"></i>
                {{ post.quantity }}
              </div>
            </div>
          </div>
        </div>
      </article>
    </transition-group>

    <el-empty v-if="!loading && articles.length === 0" description="暂无文章" />

    <div class="pagination-box">
      <el-pagination background v-if="articles.length" @current-change="$emit('page-change', $event)"
        :current-page="params.pageNum" :page-size="params.pageSize" layout="prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { formatTime } from '@/utils/time'
import UserProfileCard from '@/components/UserProfileCard/index.vue'

export default {
  name: 'ArticleList',
  components: {
    UserProfileCard
  },
  props: {
    articles: {
      type: Array,
      required: true
    },
    loading: {
      type: Boolean,
      default: false
    },
    total: {
      type: Number,
      default: 0
    },
    params: {
      type: Object,
      default: () => ({
        pageNum: 1,
        pageSize: 10
      })
    }
  },
  data() {
    return {
      popoverVisible: {}
    }
  },
  methods: {
    formatTime(time) {
      return formatTime(time)
    },
    handleCategoryClick(categoryName) {
      if (categoryName) {
        this.$emit('category-click', categoryName)
      }
    },
    togglePopover(postId) {
      // 关闭所有其他popover，只保留当前的
      Object.keys(this.popoverVisible).forEach(key => {
        if (parseInt(key) !== postId) {
          this.$set(this.popoverVisible, key, false)
        }
      })
      // 切换当前popover的显示状态
      this.$set(this.popoverVisible, postId, !this.popoverVisible[postId])
    },
    closePopover(postId) {
      this.$set(this.popoverVisible, postId, false)
    }
  }
}
</script>

<style lang="scss" scoped>
@use 'sass:color';
@use '@/styles/variables.scss' as *;
@use '@/styles/mixins.scss' as *;

.posts-list {
  display: flex;
  flex-direction: column;
  gap: $spacing-md * 1.8;
}

.post-item {
  @include card;
  background: var(--card-bg);
  overflow: hidden;
  border-radius: 16px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
  }
}

.post-main {
  position: relative;
  min-height: 240px;
  display: flex;
}

.post-text-wrapper {
  width: 62%;
  position: relative;
  z-index: 2;
  background: var(--card-bg);
}

.post-text {
  padding: $spacing-lg;
  display: flex;
  flex-direction: column;
  height: 100%;
}

// 奇数文章布局 - 文字左图片右
.post-item-odd .post-main {
  flex-direction: row;

  .post-text-wrapper {
    clip-path: polygon(0 0, 93% 0, 100% 100%, 0 100%);
    background-color: var(--card-bg);
    position: relative;
    z-index: 3;

    &::after {
      content: '';
      position: absolute;
      right: 0;
      top: 0;
      height: 100%;
      width: 20%;
      background: linear-gradient(to right, var(--card-bg), var(--card-bg));
      z-index: -1;
    }

    .post-text {
      padding-right: $spacing-lg * 2.5;
    }
  }

  .post-image {
    position: absolute;
    right: 0;
    top: 0;
    width: 45%;
    height: 100%;
    z-index: 1;

    .image-container {
      clip-path: polygon(10% 0, 100% 0, 100% 100%, 0 100%);
    }
  }
}

// 偶数文章布局 - 图片左文字右
.post-item-even .post-main {
  flex-direction: row-reverse;

  .post-text-wrapper {
    clip-path: polygon(7% 0, 100% 0, 100% 100%, 0% 100%);
    background-color: var(--card-bg);
    position: relative;
    z-index: 3;

    &::after {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      width: 20%;
      background: linear-gradient(to left, var(--card-bg), var(--card-bg));
      z-index: -1;
    }

    .post-text {
      padding-left: $spacing-lg * 2.5;
    }
  }

  .post-image {
    position: absolute;
    left: 0;
    top: 0;
    width: 45%;
    height: 100%;
    z-index: 1;

    .image-container {
      clip-path: polygon(0 0, 90% 0, 100% 100%, 0 100%);
    }
  }
}

.post-image {
  overflow: hidden;
  cursor: pointer;

  .image-container {
    position: relative;
    width: 100%;
    height: 100%;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.6s ease, filter 0.4s ease;
      filter: brightness(0.9) contrast(1.05);
    }

    .image-overlay {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(to right, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0));
      z-index: 1;
      opacity: 0.7;
    }

    .read-more-on-image {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: white;
      text-shadow: 0 1px 10px rgba(0, 0, 0, 0.5);
      font-size: 1.1rem;
      font-weight: 500;
      display: flex;
      align-items: center;
      gap: 8px;
      opacity: 0;
      transition: opacity 0.4s ease;
      z-index: 3;
      pointer-events: none;
    }
  }

  &:hover {
    img {
      transform: scale(1.08);
      filter: brightness(1) contrast(1.1);
    }

    .read-more-on-image {
      opacity: 1;
    }
  }
}

.post-text {
  h3 {
    font-size: 1.4em;
    font-weight: 700;
    margin-bottom: $spacing-md;
    line-height: 1.3;
    color: var(--text-primary);

    [data-theme='dark'] & {
      color: rgba(255, 255, 255, 0.9);
    }

    .stick-tag {
      display: inline-flex;
      align-items: center;
      gap: 4px;
      font-size: 0.5em;
      background: linear-gradient(135deg, $secondary, color.adjust($secondary, $lightness: -10%));
      color: white;
      padding: 2px 8px;
      border-radius: 4px;
      margin-right: $spacing-sm;
      vertical-align: middle;
      transform: translateY(-2px);
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);

      i {
        transform: rotate(45deg);
      }
    }
  }

  .post-title {
    transition: color 0.3s ease;
    position: relative;
    display: inline;
    cursor: pointer;

    &:hover {
      color: $primary;
    }

    &::after {
      content: '';
      position: absolute;
      bottom: -4px;
      left: 0;
      width: 0;
      height: 2px;
      background-color: $primary;
      transition: width 0.3s ease;
    }

    &:hover::after {
      width: 100%;
    }
  }

  .post-excerpt {
    color: var(--text-secondary);
    line-height: 1.7;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: 0.95em;
    margin-bottom: $spacing-md;
    letter-spacing: 0.3px;
    text-align: justify;
    flex-grow: 1;

    [data-theme='dark'] & {
      color: rgba(255, 255, 255, 0.75);
    }
  }

  .post-meta {
    display: flex;
    align-items: center;
    gap: $spacing-md;
    margin-bottom: $spacing-md;

    .category-tag {
      padding: 3px 12px;
      background: var(--hover-bg);
      border-radius: 20px;
      font-size: 0.85em;
      color: var(--text-secondary);
      transition: all 0.3s ease;
      cursor: pointer;

      &:hover {
        background: rgba($primary, 0.15);
        color: $primary;
        transform: translateY(-2px);
        box-shadow: 0 2px 6px rgba($primary, 0.2);
      }
    }

    .read-time {
      color: var(--text-secondary);
      font-size: 0.85em;
      display: flex;
      align-items: center;
      gap: 4px;

      i {
        color: #2fa9e1;
      }
    }
  }

  .read-more {
    display: inline-flex;
    align-items: center;
    color: $primary;
    font-size: 0.9em;
    font-weight: 500;
    gap: $spacing-sm;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    padding: 4px 0;
    align-self: flex-start;

    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 0;
      height: 1px;
      background-color: $primary;
      transition: width 0.3s ease;
    }

    &:hover {
      color: color.adjust($primary, $lightness: 10%);
      transform: translateX(5px);

      &::after {
        width: 100%;
      }

      i {
        transform: translateX(3px);
      }
    }

    i {
      font-size: 0.85em;
      transition: transform 0.3s ease;
    }
  }
}

.post-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: $spacing-md $spacing-lg;
  border-top: 1px solid var(--border-color);
  color: var(--text-secondary);
  background: var(--card-bg);

  .author-info {
    display: flex;
    align-items: center;
    gap: $spacing-sm;
    cursor: pointer;

    .author-name {
      font-weight: 500;
      color: $primary;
      font-size: 0.9em;
      transition: color 0.2s ease;

      &:hover {
        opacity: 0.85;
      }
    }

    .el-avatar {
      border: 2px solid rgba($primary, 0.2);
      transition: transform 0.2s ease;

      &:hover {
        transform: scale(1.1);
      }
    }
  }

  .post-info {
    display: flex;
    align-items: center;
    gap: $spacing-lg;
    font-size: 0.9em;

    .post-date,
    .post-view {
      display: flex;
      align-items: center;
      gap: 4px;
    }

    .fa-calendar {
      color: $primary;
    }

    .fa-eye {
      color: #67c23a;
    }
  }
}

// 移动端适配
@include responsive(md) {

  .post-item-odd .post-main,
  .post-item-even .post-main {
    flex-direction: column;
    min-height: auto;

    .post-text-wrapper {
      width: 100%;
      clip-path: none;
      order: 2;
    }

    .post-text {
      padding: $spacing-md !important;
    }

    .post-image {
      position: relative;
      width: 100%;
      height: 220px;
      order: 1;

      .image-container {
        clip-path: none;
      }
    }
  }

  .post-footer {
    flex-direction: column;
    gap: $spacing-sm;
    align-items: flex-start;

    .post-info {
      width: 100%;
      justify-content: space-between;
      margin-top: $spacing-sm;
    }
  }
}

@include responsive(sm) {
  .post-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: $spacing-sm;
  }

  .post-item .post-main .post-image {
    height: 180px;
  }
}

// 保持动画效果
.post-list-enter-active {
  transition: all 0.8s cubic-bezier(0.4, 0, 0.2, 1);
  transition-delay: calc(0.1s * var(--index));
  opacity: 0;
  transform: translateY(30px);
}

.post-list-enter-to {
  opacity: 1;
  transform: translateY(0);
}

.post-list-leave-active {
  transition: all 0.6s ease;
  position: absolute;
}

.post-list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}

.pagination-box {
  margin-top: $spacing-lg;
  display: flex;
  justify-content: center;

  .el-pagination {
    padding: $spacing-sm;
    background: var(--card-bg);
    border-radius: 30px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

    .btn-prev,
    .btn-next {
      background: transparent;

      &:hover {
        color: $primary;
      }
    }

    .el-pager li {
      background: transparent;
      transition: all 0.3s ease;

      &.active {
        color: white;
        background: $primary;
        font-weight: bold;
      }

      &:hover:not(.active) {
        color: $primary;
      }
    }
  }
}

/* Popover a little style adjustment */
.user-popover {
  padding: 0 !important;
  border-radius: 8px;
  border: 1px solid var(--border-color) !important;
}

.summary-popover {
  max-height: 300px;
  overflow-y: auto;
  word-break: break-word;
  background: var(--card-bg);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 16px;
  font-size: 14px;
  line-height: 1.6;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);

  [data-theme='dark'] & {
    background: #2d3748;
    color: rgba(255,255,255,0.9);
    border-color: #4a5568;
  }
}

.post-item:has(img[lazy=loading]),
.post-item:has(img[lazy=error]) {
  .post-image .image-container {
    background-color: #e6e6e6;
    animation: pulse-bg 1.5s infinite;

    img {
      visibility: hidden;
    }
  }

  .post-text {

    .post-title,
    .category-tag,
    .read-time {
      color: transparent !important;
      background-color: #e6e6e6;
      animation: pulse-bg 1.5s infinite;
      border-radius: 4px;
    }

    .post-title {
      min-height: 1.4em;
      width: 80%;
      display: inline-block;
    }

    .post-excerpt {
      color: transparent !important;
      -webkit-line-clamp: 3;
      display: block;
      height: calc(1.7em * 3);
      overflow: hidden;

      &::before {
        content: '';
        display: block;
        width: 100%;
        height: 1em;
        background: #e6e6e6;
        border-radius: 4px;
        margin-bottom: 0.7em;
        animation: pulse-bg 1.5s infinite;
        box-shadow: 0 1.7em 0 0 #e6e6e6, 0 3.4em 0 0 #e6e6e6;
      }
    }

    .category-tag {
      min-width: 80px;
    }

    .read-time {
      min-width: 100px;
    }
  }
}

@keyframes pulse-bg {

  0%,
  100% {
    opacity: 0.5;
  }

  50% {
    opacity: 0.8;
  }
}
</style>